﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace ManageTicket.Partner
{
    public partial class frmProvider : frmBase
    {
        #region Contructor
        public frmProvider()
        {
            InitializeComponent();
        }
        #endregion

        #region Variable
        ManageTicket.Library.Control.ExecuteControl exeCtrl;

        //permission
        bool bAdd = false;
        bool bEdit = false;
        bool bDelete = false;
        bool bCancel = false;
        bool bSave = false;
        bool bFunction = false;
        bool bView = false;
        bool bFind = false;
        bool bExport = false;
        bool bSendmail = false;
        bool bPrint = false;

        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();
        private Business.Provider boProvider = new Business.Provider();
        private Business.ProviderService boProSer = new Business.ProviderService();
        #endregion

        #region Event
        private void frmProvider_Load(object sender, EventArgs e)
        {
            //execute control
            exeCtrl = new ManageTicket.Library.Control.ExecuteControl();
            exeCtrl.Dock = DockStyle.Fill;
            exeCtrl.lblDescription.Text = "Cập nhật danh sách nhà cung cấp";
            pnlExe.Controls.Add(exeCtrl);

            //permission
            clsCommonFunc.GetPermission(ref bAdd, ref bEdit, ref bDelete, ref bCancel, ref bSave, ref bFunction, ref bView, ref bFind, ref bExport, ref bSendmail, ref bPrint, frmMain.CurrentUser.UserId, "FORM_06");

            //get code type
            clsCommonFunc.GetListCodeType(cboBank, "BANK");
            clsCommonFunc.GetListCodeType(cboPersonRepresentNationality, "NATION");
            clsCommonFunc.GetListCodeType(cboPersonContactNationality, "NATION");
            
            //get service
            LoadService();

            //set status
            LoadStatusOnce();
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                ArrayList arrResult = new ArrayList();
                if (objMethod.IsInteger(txtSearch.Text))
                {
                    DataObjects.Provider oPro = boProvider.GetObjectByPK(txtSearch.Text);
                    if (oPro != null)
                    {
                        arrResult.Add(oPro);
                    }
                }
                else
                {
                    ArrayList arr1 = boProvider.GetListQuickSearch(txtSearch.Text);
                    if (arr1 != null)
                    {
                        if (arr1.Count == 1)
                        {
                            arrResult.Add(arr1[0]);
                        }
                        else if (arr1.Count > 1)
                        {
                            frmQuickSearchProvider frmQS = new frmQuickSearchProvider(arr1);
                            frmQS.ShowDialog();
                            foreach (DataObjects.Provider oPro in frmQS.arrResult)
                            {
                                arrResult.Add(oPro);
                            }
                        }
                    }
                }

                ListViewItem item;
                int firstItem = 0;
                foreach (DataObjects.Provider oPro in arrResult)
                {
                    if (!IsInListViewProvider(oPro))
                    {
                        item = new ListViewItem();
                        item.Text = oPro.ProviderId;
                        item.SubItems.Add(oPro.ProviderName);
                        lvwProvider.Items.Add(item);
                        if (firstItem == 0)
                        {
                            item.Selected = true;
                        }
                        firstItem++;
                    }
                }
            }
        }

        private void lvwProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwProvider.SelectedItems.Count > 0)
            {
                DataObjects.Provider oPro = boProvider.GetObjectByPK(lvwProvider.SelectedItems[0].Text);
                if (oPro != null)
                {
                    FillData(oPro);
                    LoadStatus(true, true, true, true);
                }
            }
            else
            {
                LoadStatus(true, false, true, true);
            }
        }
        #endregion

        #region Execute
        public override void Add()
        {
            txtProviderId.Text = "";
            txtProviderId.ReadOnly = false;
            objMethod.ClearText(new Control[] { txtProviderId, txtProviderName, txtAddress, txtPhoneWork, txtPhoneExtension, txtFax, txtTaxCode, txtBankAccount, txtRemark, txtPersonRepresent, txtPersonRepresentMobile, txtPersonRepresentEmail, txtPersonContact, txtPersonContactMobile, txtPersonContactEmail });
            ClearService();
            txtProviderId.Focus();
        }

        public override void Delete()
        {
            try
            {
                if (txtProviderId.Text == "")
                    return;

                if (MessageBox.Show("Bạn có thực sự muốn xóa?", "Aquarius", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //Check Existing in table
                    //if (this.CheckExitToDelete(oDept.DEPT_CODE)) {
                    //    MessageBox.Show(".", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    return;
                    //}

                    //delete service
                    boProSer.DeleteByProId(txtProviderId.Text);
                    //delete
                    boProvider.DeleteByPK(txtProviderId.Text);
                    RemoveItem(txtProviderId.Text);
                    objMethod.ClearText(new Control[] { txtProviderId, txtProviderName, txtAddress, txtPhoneWork, txtPhoneExtension, txtFax, txtTaxCode, txtBankAccount, txtRemark, txtPersonRepresent, txtPersonRepresentMobile, txtPersonRepresentEmail, txtPersonContact, txtPersonContactMobile, txtPersonContactEmail });
                    ClearService();
                    MessageBox.Show("Xóa nhà cung cấp thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override void Save()
        {
            if (txtProviderId.Text == "")
            {
                return;
            }
            if (CheckValid())
            {
                try
                {
                    DataObjects.Provider oPro = new DataObjects.Provider();
                    oPro.ProviderId = txtProviderId.Text;
                    oPro.ProviderName = txtProviderName.Text;
                    oPro.Address = txtAddress.Text;
                    oPro.PhoneWork = txtPhoneWork.Text;
                    oPro.PhoneExtension = txtPhoneExtension.Text;
                    oPro.Fax = txtFax.Text;
                    oPro.TaxCode = txtTaxCode.Text;
                    oPro.BankAccount = txtBankAccount.Text;
                    oPro.BankCode = cboBank.SelectedValue != null ? cboBank.SelectedValue.ToString() : "";
                    oPro.Remark = txtRemark.Text;
                    oPro.PersonRepresent = txtPersonRepresent.Text;
                    oPro.PersonRepresentNational = cboPersonRepresentNationality.SelectedValue != null ? cboPersonRepresentNationality.SelectedValue.ToString() : "";
                    oPro.PersonRepresentMobile = txtPersonRepresentMobile.Text;
                    oPro.PersonRepresentEmail = txtPersonRepresentEmail.Text;
                    oPro.PersonContact = txtPersonContact.Text;
                    oPro.PersonContactNational = cboPersonContactNationality.SelectedValue != null ? cboPersonContactNationality.SelectedValue.ToString() : "";
                    oPro.PersonContactMobile = txtPersonContactMobile.Text;
                    oPro.PersonContactEmail = txtPersonContactEmail.Text;
                    oPro.Status = "CUR";

                    if (txtProviderId.ReadOnly)
                    {
                        //update
                        boProvider.Update(oPro);
                        RemoveItem(oPro.ProviderId);
                        AddItem(oPro);
                    }
                    else
                    {
                        //insert
                        if (CheckIsExist(oPro.ProviderId))
                        {
                            MessageBox.Show("Mã nhà cung cấp đã tồn tại.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtProviderId.Focus();
                            return;
                        }
                        boProvider.Insert(oPro);
                        AddItem(oPro);
                    }
                    SaveService(oPro.ProviderId);
                    txtProviderId.ReadOnly = true;
                    MessageBox.Show("Cập nhật nhà cung cấp thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + "Không thể cập nhật nhà cung cấp.", "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        public override void Find()
        {
            frmSearchProvider frmSearch = new frmSearchProvider();
            frmSearch.ShowDialog();

            ArrayList arrResult = frmSearch.arrResult;
            ListViewItem item;
            int firstItem = 0;

            foreach (DataObjects.Provider oPro in arrResult)
            {
                if (!IsInListViewProvider(oPro))
                {
                    item = new ListViewItem();
                    item.Text = oPro.ProviderId;
                    item.SubItems.Add(oPro.ProviderName);
                    lvwProvider.Items.Add(item);

                    if (firstItem == 0)
                    {
                        item.Selected = true;
                    }
                    firstItem++;
                }
            }
        }
        #endregion

        #region Method
        private bool IsInListViewProvider(DataObjects.Provider oPro)
        {
            foreach (ListViewItem item in lvwProvider.Items)
            {
                if (item.Text == oPro.ProviderId)
                {
                    return true;
                }
            }
            return false;
        }

        private void LoadStatusOnce()
        {
            exeCtrl.btnEdit.Enabled = false;
            exeCtrl.btnCancel.Enabled = false;
            exeCtrl.btnView.Enabled = false;
            exeCtrl.btnFunction.Enabled = false;
            exeCtrl.btnExport.Enabled = false;
            exeCtrl.btnSendmail.Enabled = false;
            exeCtrl.btnPrint.Enabled = false;

            exeCtrl.btnEdit.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnCancel.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnView.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnFunction.BackColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnExport.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnSendmail.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnPrint.ButtonColor = Color.FromArgb(171, 171, 171);

            exeCtrl.btnEdit.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnCancel.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnView.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnFunction.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnExport.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnSendmail.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnPrint.ForeColor = Color.FromArgb(241, 244, 251);
        }

        private void LoadStatus(bool blAdd, bool blDelete, bool blSave, bool blFind)
        {
            exeCtrl.btnAdd.Enabled = blAdd & bAdd;
            exeCtrl.btnDelete.Enabled = blDelete & bDelete;
            exeCtrl.btnSave.Enabled = blSave & bSave;
            exeCtrl.btnFind.Enabled = blFind & bFind;


            exeCtrl.btnAdd.ButtonColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnDelete.ButtonColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnSave.ButtonColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnFind.ButtonColor = exeCtrl.btnFind.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);

            exeCtrl.btnAdd.ForeColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnDelete.ForeColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnSave.ForeColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnFind.ForeColor = exeCtrl.btnFind.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
        }

        private void FillData(DataObjects.Provider oPro)
        {
            try
            {
                txtProviderId.ReadOnly = true;
                txtProviderId.Text = oPro.ProviderId;
                txtProviderName.Text = oPro.ProviderName;
                //person represent
                txtPersonRepresent.Text = oPro.PersonRepresent;
                if (oPro.PersonRepresentNational != null && oPro.PersonRepresentNational != "")
                    cboPersonRepresentNationality.SelectedValue = oPro.PersonRepresentNational;
                txtPersonRepresentMobile.Text = oPro.PersonRepresentMobile;
                txtPersonRepresentEmail.Text = oPro.PersonRepresentEmail;
                //person contact
                txtPersonContact.Text = oPro.PersonContact;
                if (oPro.PersonContactNational != null && oPro.PersonContactNational != "")
                    cboPersonContactNationality.SelectedValue = oPro.PersonContactNational;
                txtPersonContactMobile.Text = oPro.PersonContactMobile;
                txtPersonContactEmail.Text = oPro.PersonContactEmail;
                txtAddress.Text = oPro.Address;
                txtPhoneWork.Text = oPro.PhoneWork;
                txtPhoneExtension.Text = oPro.PhoneExtension;
                txtFax.Text = oPro.Fax;
                txtTaxCode.Text = oPro.TaxCode;
                txtBankAccount.Text = oPro.BankAccount;
                if (oPro.BankCode != null && oPro.BankCode != "")
                    cboBank.SelectedValue = oPro.BankCode;
                txtRemark.Text = oPro.Remark;
                FillService(oPro.ProviderId);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool CheckValid()
        {
            if (txtProviderId.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng nhập mã nhà cung cấp.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtProviderId.Focus();
                return false;
            }
            else if (!objMethod.CheckSpecialCharacter(txtProviderId.Text))
            {
                MessageBox.Show("Mã nhà cung cấp không được nhập các ký tự đặc biệt.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtProviderId.Focus();
                return false;
            }

            if (txtProviderName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng nhập tên công ty.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtProviderName.Focus();
                return false;
            }
            if (txtPersonRepresentEmail.Text.Trim().Length > 0)
            {
                if (!objMethod.CheckEmail(txtPersonRepresentEmail.Text))
                {
                    MessageBox.Show("Email người đại diện không hợp lệ.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPersonRepresentEmail.Focus();
                    return false;
                }
            }
            if (txtPersonContactEmail.Text.Trim().Length > 0)
            {
                if (!objMethod.CheckEmail(txtPersonContactEmail.Text))
                {
                    MessageBox.Show("Email người liên lạc không hợp lệ.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPersonContactEmail.Focus();
                    return false;
                }
            }
            if (lvwService.CheckedItems.Count == 0)
            {
                MessageBox.Show("Vui lòng chọn loại dịch vụ.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                lvwService.Focus();
                return false;
            }

            return true;
        }

        private void AddItem(DataObjects.Provider oPro)
        {
            ListViewItem item = new ListViewItem();
            item.Text = oPro.ProviderId;
            item.SubItems.Add(oPro.ProviderName);
            lvwProvider.Items.Add(item);
        }

        private void RemoveItem(string proId)
        {
            foreach (ListViewItem item in lvwProvider.Items)
            {
                if (item.Text == proId)
                {
                    lvwProvider.Items.Remove(item);
                    return;
                }
            }
        }

        private void ClearService()
        {
            foreach (ListViewItem item in lvwService.Items)
            {
                item.Checked = false;
            }
        }

        private void LoadService()
        {
            Business.CodeType boCodeType = new Business.CodeType();
            ArrayList arr = boCodeType.GetListCodeTypeByGroup("SERV");
            if (arr != null)
            {
                if (arr.Count > 0)
                {
                    lvwService.Items.Clear();
                    ListViewItem item;
                    foreach (DataObjects.CodeType oSer in arr)
                    {
                        item = new ListViewItem();
                        item.Tag = oSer.CodeTypeId;
                        item.Text = oSer.CodeTypeName;
                        lvwService.Items.Add(item);
                    }
                }
            }
        }

        private void FillService(string proId)
        {
            ClearService();
            ArrayList arr = boProSer.GetListByProId(proId);
            if (arr != null)
            {
                if (arr.Count > 0)
                {
                    foreach (DataObjects.ProviderService oProSer in arr)
                    {
                        foreach (ListViewItem item in lvwService.Items)
                        {
                            if (oProSer.ServiceId == item.Tag.ToString())
                            {
                                item.Checked = true;
                            }
                        }
                    }
                }
            }
        }

        private void SaveService(string proId)
        {
            //delete
            boProSer.DeleteByProId(proId);

            //insert
            DataObjects.ProviderService oProSer;
            foreach (ListViewItem item in lvwService.CheckedItems)
            {
                oProSer = new DataObjects.ProviderService();
                oProSer.ProviderId = proId;
                oProSer.ServiceId = item.Tag.ToString();
                boProSer.Insert(oProSer);
            }
        }
        
        private bool CheckIsExist(string proId)
        {
            DataObjects.Provider oPro = boProvider.GetObjectByPK(proId);
            if (oPro != null)
            {
                return true;
            }
            return false;
        }
        #endregion

    }
}
